package org.example.clouddemo.controller.app;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.example.clouddemo.bean.common.entity.Product;
import org.example.clouddemo.bean.common.param.CreateOrderParam;
import org.example.clouddemo.feign.ProductFeignClient;
import org.example.clouddemo.service.common.UserService;
import org.example.common.annotation.IgnoreAuth;
import org.example.common.exception.AppRunTimeException;
import org.example.common.response.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 订单接口
 * </p>
 *
 * @author hzg
 * @since 2025-06-08
 */

@Api(tags = "订单接口")
@ApiSupport(order = 2)
@RestController("appOrderController")
@RequestMapping("/api/app/order")
@Log4j2
public class OrderController {

    @Autowired
    public UserService userService;

    @Autowired
    public ProductFeignClient productFeignClient;

    @ApiOperation(value = "创建订单", notes = "返回结果是订单id")
    @ApiOperationSupport(order = 1)
    @PostMapping("createOrder")
    public ApiResult<Long> createOrder(@Validated @RequestBody CreateOrderParam param) {

        //TODO: 暂时把逻辑写在这里,演示用, 后续应该放在对应的service中


        //获取产品信息

        Product find = new Product();
        find.setId(param.getProductId());
        ApiResult<Product> productApiResult = productFeignClient.getProductById(find);

        if (!productApiResult.isSuccess()) {
            throw new AppRunTimeException("找不到产品");
        }

        Product product = productApiResult.data;
        log.info("getProductById:{}", product);

        Product product2 = productFeignClient.getProductById2(find);

        log.info("getProductById2:{}", product2);

        //TODO:订单逻辑,现在虚构一个订单id
        Long orderId = IdUtil.getSnowflake().nextId();

        return ApiResult.success(orderId);
    }


    @ApiOperation(value = "测试feign调用", notes = "远程调用service-product的/api/app/product/queryOne的接口")
    @ApiOperationSupport(order = 99)
    @PostMapping("test")
    @IgnoreAuth
    public ApiResult<JSONObject> test(@Validated @RequestBody CreateOrderParam param) {

        //获取产品信息

        Product find = new Product();
        find.setId(param.getProductId());
        ApiResult<Product> productApiResult = productFeignClient.getProductById(find);

        if (!productApiResult.isSuccess()) {
            throw new AppRunTimeException("找不到产品");
        }

        Product product = productApiResult.data;
        log.info("getProductById:{}", product);

        Product product2 = productFeignClient.getProductById2(find);

        log.info("getProductById2:{}", product2);


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("productId", param.getProductId());
        jsonObject.put("getProductById", productApiResult);
        jsonObject.put("getProductById2", product2);
        return ApiResult.success(jsonObject);
    }
}
